﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.UI.Xaml;
using Windows.Storage.Streams;
using System.IO;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using BoolanSlide.UIElement.Picture;
using System.ComponentModel;
using Windows.Foundation;


namespace BoolanElement
{
    public class BooPictureElement:BooElement
    {
        public string filePath;
        private double _ImageWidth;
        public double ImageWidth
        {
            get 
            {
                return _ImageWidth;
            }
            set
            {
                _ImageWidth = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ChangeImageWidth"));
            }
        }
        private double _ImageHeight;
        public double ImageHeight
        {
            get
            {
                return _ImageHeight;
            }
            set
            {
                _ImageHeight = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ChangeImageHeight"));
            }
        }

        private double _ViewWidth;
        public double ViewWidth
        {
            get
            {
                return _ViewWidth;
            }
            set
            {
                _ViewWidth = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ChangeViewWidth"));
            }
        }
        private double _ViewHeight;
        public double ViewHeight
        {
            get
            {
                return _ViewHeight;
            }
            set
            {
                _ViewHeight = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ChangeViewHeight"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public ScaleTransform     ViewScaleTransform;
        public RotateTransform    ViewRotateTransform;
        public TranslateTransform ViewTranslateTransform;

        public ScaleTransform     ImageScaleTransform;
        public RotateTransform    ImageRotateTransform;
        public TranslateTransform ImageTranslateTransform;

        PictureElementLoadAndSaveHelper elementLoadAndSaveHelper;

        public const double InvalidValue = -1.0;

        private Rect viewRect;
        public Rect ViewRect
        {
            set
            {
                viewRect = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ClipImage"));
            }
        }

        public int ZIndex
        {
            set
            {
                ElementTransform.ZIndex = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ChangeZIndex"));
            }
        }
        
        public BooPictureElement()
        {
            ImageWidth = InvalidValue;
            ImageHeight = InvalidValue;

            ViewWidth = InvalidValue;
            ViewHeight = InvalidValue;
            InitializeTransforms();

            elementLoadAndSaveHelper = new PictureElementLoadAndSaveHelper(this);
        }

        public BooPictureElement(BooPictureElement picElement)
        {
            elementLoadAndSaveHelper = new PictureElementLoadAndSaveHelper(this);

            this.ImageWidth = picElement.ImageWidth;
            this.ImageHeight = picElement.ImageHeight;
            this.ViewWidth = picElement.ViewWidth;
            this.ViewHeight = picElement.ViewHeight;
            InitializeTransforms();

            this.ElementTransform = picElement.ElementTransform.Clone();
            this.filePath = picElement.filePath;


            ViewScaleTransform.CenterX = picElement.ViewScaleTransform.CenterX;
            ViewScaleTransform.CenterY = picElement.ViewScaleTransform.CenterY;
            ViewScaleTransform.ScaleX = picElement.ViewScaleTransform.ScaleX;
            ViewScaleTransform.ScaleY = picElement.ViewScaleTransform.ScaleY;


            ViewRotateTransform.Angle = picElement.ViewRotateTransform.Angle;
            ViewRotateTransform.CenterX = picElement.ViewRotateTransform.CenterX;
            ViewRotateTransform.CenterY = picElement.ViewRotateTransform.CenterY;


            ViewTranslateTransform.X = picElement.ViewTranslateTransform.X;
            ViewTranslateTransform.Y = picElement.ViewTranslateTransform.Y;

            ImageScaleTransform.CenterX = picElement.ImageScaleTransform.CenterX;
            ImageScaleTransform.CenterY = picElement.ImageScaleTransform.CenterY;
            ImageScaleTransform.ScaleX = picElement.ImageScaleTransform.ScaleX;
            ImageScaleTransform.ScaleY = picElement.ImageScaleTransform.ScaleY;


            ImageRotateTransform.Angle = picElement.ImageRotateTransform.Angle;
            ImageRotateTransform.CenterX = picElement.ImageRotateTransform.CenterX;
            ImageRotateTransform.CenterY = picElement.ImageRotateTransform.CenterY;

            ImageTranslateTransform.X = picElement.ImageTranslateTransform.X;
            ImageTranslateTransform.Y = picElement.ImageTranslateTransform.Y;
        }

        private void InitializeTransforms()
        {
            ViewScaleTransform = new ScaleTransform() 
            {
                CenterX = 0,
                CenterY = 0
            };
            ViewRotateTransform = new RotateTransform() 
            {
                Angle = 0,
                CenterX = 0,
                CenterY = 0
            };
            ViewTranslateTransform = new TranslateTransform() 
            {
                X = 0,
                Y = 0
            };

            ImageScaleTransform = new ScaleTransform() 
            {
                CenterX = 0,
                CenterY = 0,
                ScaleX = 1.0,
                ScaleY = 1.0
            };
            ImageRotateTransform = new RotateTransform() 
            {
                Angle = 0,
                CenterX = 0,
                CenterY = 0
            };
            ImageTranslateTransform = new TranslateTransform() 
            {
                X = 0,
                Y = 0                
            };
        }

        public override async Task<BooUIBase> Render(SaveOriginalElement saveOriginal, SaveForRedoUndo saveForRedoUndo)
        {
            BooPicture pictureTemp = new BooPicture(this, saveForRedoUndo);
            await pictureTemp.ShowPicture( );

            return pictureTemp;
        }

        public override async Task<BooUIBase> RenderForThumbNails()
        {
            BooPicture pictureTemp = new BooPicture(this);
            await pictureTemp.ShowPicture();

            return pictureTemp;
        }

        public override XElement SaveToXElement()
        {
            return elementLoadAndSaveHelper.SaveToXElement();
        }

        public override void LoadFromXElement(XElement element)
        {
            elementLoadAndSaveHelper.LoadFromXElement(element);
        }       

        public override BooElement Clone()
        {
            BooPictureElement elementReturn = new BooPictureElement(this);

            elementReturn.ElementTransform = this.ElementTransform.Clone();

            return elementReturn;
        }
    }
}
